package com.fmatos.amazingwallpapers.service.image;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.fmatos.amazingwallpapers.image.database.ImagesDataSource;
import com.fmatos.amazingwallpapers.image.database.dto.ImageRow;
import com.fmatos.amazingwallpapers.imagemanagement.BitmapsDownloadManager;

public class BitmapsWorker extends HandlerThread {

	private static final String TAG = BitmapsWorker.class.getSimpleName();
	
	private Handler hDownloader;
	private ImagesDataSource dataSource;
	private Context context;
	
	public BitmapsWorker(String name, ImagesDataSource dataSource,Context context) {
		super(name);
		this.dataSource = dataSource;
		this.context = context;
	}

	@Override
	protected void onLooperPrepared() {
		super.onLooperPrepared();
		hDownloader = new Handler();
	}

	/**
	 * Tries to quit handler safely
	 */
	@SuppressLint("NewApi")
	public void stopHandler() {
		if ( hDownloader != null && hDownloader.getLooper() != null) {
			hDownloader.getLooper().quitSafely();
		}
		quitSafely();
	}

	public final boolean sendMessageAtFrontOfQueue(Message msg) {
		return hDownloader
				.sendMessageAtFrontOfQueue(msg);
	}	

		
	private int downloadACoupleOfImages (int amountOfImages) {
		BitmapsDownloadManager downloader = new BitmapsDownloadManager();
//		int count = downloader.downloadImagesWithoutFile(dataSource,context,amountOfImages);
		
		
		return 0;
	}

	public final boolean getAlbumsInfo() {
		
		Log.d(TAG,"Starting to download albums");
		if (hDownloader == null) {
			return false;
		}
		
		return hDownloader.post(new Runnable() {
			@Override
			public void run() {
//				BitmapsDownloadManager downloader = new BitmapsDownloadManager();
//				int newPhotosFound = downloader.downloadAllImagesFromAlbum(dataSource,context);
//				Toast.makeText(context, "Finished downloading album, found " + newPhotosFound + " new photos.", Toast.LENGTH_SHORT).show();
//				Toast.makeText(context, "Download album info", Toast.LENGTH_SHORT).show();
//				Log.d(TAG,"Finishing to download albums");
			}
		});
	}
	
	public final boolean getNewestImages() {
		
		Log.d(TAG,"Starting to download images");
		if (hDownloader == null) {
			return false;
		}
		
		boolean isPosted =  hDownloader.post(new Runnable() {
			
			@Override
			public void run() {
				int count;
				Log.d(TAG,"Looper: download images");
				count = downloadACoupleOfImages(15);
				if ( count == 0 ) {
					BitmapsDownloadManager downloader = new BitmapsDownloadManager();
					int newPhotosFound = downloader.downloadAllImagesFromAlbum(dataSource,context);
					Toast.makeText(context, "Finished downloading album, found " + newPhotosFound + " new photos.", Toast.LENGTH_SHORT).show();
				}
				

			}
		});
		
		if ( ! isPosted ) {
			Log.d(TAG,"Looper failed to post images, service is down");
		}
		
		return isPosted;
	}
	
	/**
	 * Sends a stopSelf() method call to the end of the handler queue
	 * @param service
	 * @return
	 */
	@SuppressLint("NewApi")
	public boolean stopServiceDelayed(final BitmapsService service) {
		

		if (hDownloader == null) {
			return false;
		}
		
		hDownloader.post(new Runnable() {
			
			@Override
			public void run() {

				Toast.makeText(context, "shutdown service ", Toast.LENGTH_SHORT).show();
				Log.i(TAG," shutdown service now");
				service.stopSelf();
				

			}
		});
		
		Log.i(TAG," shutdown service delayed");
//		hDownloader.getLooper().quitSafely();
//		hDownloader = null; // does this help with memory leak?
		
		return true;
		
	}


	
}
